package cc.langk.core.util;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Date: 2017/11/7
 * Time: 10:29
 * Description:
 */
public class IDBuilder {

    private static final String[] randomChar = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};


    public static String buildID() {
        StringBuilder result = new StringBuilder(System.currentTimeMillis() + "");
        result = replenishZero(result, 14);
        result = generationRandom(result, 24);
        return result.toString();
    }

    /**
     * 补充0
     *
     * @param str
     * @param length
     * @return
     */
    private static StringBuilder replenishZero(StringBuilder str, int length) {
        if (str.length() < length) {
            str.insert(0, "0");
            return replenishZero(str, length);
        } else {
            return str;
        }
    }

    private static StringBuilder generationRandom(StringBuilder str, int length) {
        if (str.length() < length) {
            Random random = new Random();
            str.insert(0, randomChar[random.nextInt(randomChar.length)]);
            return generationRandom(str, length);
        } else {
            return str;
        }
    }

}
